﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.UserCotrols
{
    public partial class CtrSelectRole : BaseControl
    {
        private Sys_RoleBLL srb = new Sys_RoleBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindListBox();
            }
        }

        private void BindListBox()
        {
            DataTable dt = srb.GetAllInfo2();

            foreach (DataRow Row in dt.Rows)
            {
                ListItem item1 = new ListItem();
                item1.Text = Row["ROLE_NAME"].ToString();
                item1.Value = Row["ROLE_ID"].ToString();
                ListBoxBOCKM.Items.Add(item1);
            }
        }

        protected void BtnRight_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ListBoxBOCKM.SelectedValue))
            {
                ListItem item = ListBoxBOCKM.SelectedItem;
                ListBox.Items.Add(item);
                ListBoxBOCKM.Items.RemoveAt(ListBoxBOCKM.SelectedIndex);
            }
        }

        protected void BtnLeft_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ListBox.SelectedValue))
            {
                ListItem item = ListBox.SelectedItem;
                ListBoxBOCKM.Items.Add(item);
                ListBox.Items.RemoveAt(ListBox.SelectedIndex);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string Role = "";
            if (ListBox.Items.Count != 0)
            {
                string name = "";
                string id = "";
                foreach (ListItem item in ListBox.Items)
                {
                    name += item.Text + ",";
                    id += item.Value + ",";
                }
                name = name.Substring(0, name.Length - 1);
                id = id.Substring(0, id.Length - 1);
                Role = id + "%" + name;
            }
            else
            {
                Role = "" + "%" + "";
            }
            MessageBox.ResponseScript(Page, "FORM_SUBMIT(\"" + Role + "\");");
        }

        protected void btnXS_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ListBox.SelectedValue))
            {
                int index = ListBox.SelectedIndex;
                if (index > 0)
                {
                    ListItem item = ListBox.SelectedItem;
                    ListBox.Items.RemoveAt(ListBox.SelectedIndex);
                    ListBox.Items.Insert(index - 1, item);
                }
            }
        }

        protected void btnXX_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ListBox.SelectedValue))
            {
                int index = ListBox.SelectedIndex;
                if (index < ListBox.Items.Count - 1)
                {
                    ListItem item = ListBox.SelectedItem;
                    ListBox.Items.RemoveAt(ListBox.SelectedIndex);
                    ListBox.Items.Insert(index + 1, item);
                }
            }
        }
    }
}